package name.sakanacatcher.controller;

import name.sakanacatcher.entity.User;
import name.sakanacatcher.service.ResumeService;
import name.sakanacatcher.service.UserService;
import name.sakanacatcher.util.ResultUtil;
import name.sakanacatcher.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Map;

@RestController
@RequestMapping("/api/resume")
public class ResumeController {

    @Autowired
    ResultUtil resultUtil;

    @Autowired
    ResumeService resumeService;

    @Autowired
    UserService userService;

    @Autowired
    TokenUtil tokenUtil;

    @GetMapping("/auth")
    public Map<String, Object> getResumeByAuth(@RequestHeader("Authorization") String authorization) {
        String username = tokenUtil.getSubject(authorization.replace("Bearer ", ""));
        int id = userService.getUserRepository().getByUsername(username).getId();
        if(resumeService.getResumeByUid(id)!= null)
            return resultUtil.successResult("Get Success", Map.of("items", resumeService.getResumeByUid(id)));
        else
            return resultUtil.successResult("Get Success", Map.of("items", ""));
    }

    @GetMapping("/id")
    public Map<String, Object> getResumeById(@RequestParam int id) {
        if(resumeService.getResumeByUid(id)!= null)
            return resultUtil.successResult("Get Success", Map.of("items", resumeService.getResumeByUid(id)));
        else
            return resultUtil.successResult("Get Success", Map.of("items", ""));
    }

    @GetMapping("/uid")
    public Map<String, Object> getHrList(@RequestParam int id) {
        return resultUtil.successResult("Get Success", Map.of("items", resumeService.getResumeRepository().getOne(id)));
    }

    @PostMapping("/update")
    public Map<String, Object> updateResume(@RequestBody Map<String,Object> data) throws ParseException {
        return resultUtil.successResult("Get Success", Map.of("items", resumeService.updateResumeByUid(data)));
    }

}
